from django import forms
from django.core.exceptions import ValidationError
from .models import UserProfile, Dept, Menu, MenuButton, Role, Dictionary, OperationLog


class LoginForm(forms.Form):
    """登录表单验证"""
    username = forms.CharField(required=True)
    password = forms.CharField(required=True)
    captcha_1 = forms.CharField(required=True, label="验证码")


class ModifyPwdForm(forms.Form):
    """修改密码表单验证"""
    pwd1 = forms.RegexField(
        # 简单
        '^(?=.*[0-9])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        # 标准
        # '^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        # 复杂
        # '^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*()])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        min_length=6,
        max_length=32,
        error_messages={
            'required': '密码不能为空',
            # 简单
            'invalid': '密码必须包含数字',
            # 标准
            # 'invalid': '密码必须包含数字、字母',
            # 复杂
            # 'invalid': '密码必须包含数字、字母、特殊字符（不含空格）',
            'min_length': '密码长度不能小于6个字符',
            'max_length': '密码长度不能大于32个字符'
        }
    )
    pwd2 = forms.RegexField(
        # 简单
        '^(?=.*[0-9])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        # 标准
        # '^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        # 复杂
        # '^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*()])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        min_length=6,
        max_length=32,
        error_messages={
            'required': '密码不能为空',
            # 简单
            'invalid': '密码必须包含数字',
            # 标准
            # 'invalid': '密码必须包含数字、字母',
            # 复杂
            # 'invalid': '密码必须包含数字、字母、特殊字符（不含空格）',
            'min_length': '密码长度不能小于6个字符',
            'max_length': '密码长度不能大于32个字符'
        }
    )

    def clean(self):
        pass
        v_pwd1 = self.cleaned_data.get('pwd1')
        v_pwd2 = self.cleaned_data.get('pwd2')
        if v_pwd1 == v_pwd2:
            pass
        else:
            raise ValidationError(message='两次新密码不一致')


class RegistForm(forms.Form):
    """注册表单验证"""
    username = forms.CharField(required=True)
    password = forms.RegexField(
        # 简单
        '^(?=.*[0-9])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        # 标准
        # '^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        # 复杂
        # '^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*()])[0-9a-zA-Z!@#$%^&*()]{6,32}$',
        min_length=6,
        max_length=32,
        error_messages={
            'required': '密码不能为空',
            # 简单
            'invalid': '密码必须包含数字',
            # 标准
            # 'invalid': '密码必须包含数字、字母',
            # 复杂
            # 'invalid': '密码必须包含数字、字母、特殊字符（不含空格）',
            'min_length': '密码长度不能小于6个字符',
            'max_length': '密码长度不能大于32个字符'
        }
    )
    dept_id = forms.CharField(required=True, error_messages={'required': '部门不能为空'})

    # 局部钩子：验证用户名是否已存在
    def clean_username(self):
        username = self.cleaned_data.get('username')
        is_exist = UserProfile.objects.filter(username=username)
        if bool(is_exist):
            raise ValidationError(message='用户名已存在', code='unique')
        return username


class UserProfileForm(forms.ModelForm):
    """用户信息表单验证"""

    class Meta:
        model = UserProfile
        exclude = ['password', 'date_joined', 'is_delete', 'creator']

    # 局部钩子：增加验证部门不能为空
    def clean_dept(self):
        dept = self.cleaned_data.get('dept')
        if dept is None:
            raise ValidationError(message='部门不能为空', code='required')
        return dept


class MenuForm(forms.ModelForm):
    """菜单表单验证"""

    class Meta:
        model = Menu
        exclude = ['is_delete', 'creator']


class DeptForm(forms.ModelForm):
    """部门表单验证"""

    class Meta:
        model = Dept
        exclude = ['is_delete', 'creator']


class DictionaryForm(forms.ModelForm):
    """部门表单验证"""

    class Meta:
        model = Dictionary
        exclude = ['is_delete', 'creator']


class RoleForm(forms.ModelForm):
    """部门表单验证"""

    class Meta:
        model = Role
        exclude = ['is_delete', 'dept', 'menu', 'permission', 'creator']


class MenuButtonForm(forms.ModelForm):
    """部门表单验证"""

    class Meta:
        model = MenuButton
        exclude = ['is_delete', 'creator']


class OperationLogForm(forms.ModelForm):
    """部门表单验证"""

    class Meta:
        model = OperationLog
        exclude = ['is_delete', 'creator']
